package com.tedu.controller;


import com.tedu.Common.web.JsonResult;
import com.tedu.pojo.entity.Comment;
import com.tedu.pojo.param.CommentParam;
import com.tedu.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@Validated
@RequestMapping("/comments")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @PostMapping("/addnew")
    @ApiOperation("新增评论")
    public JsonResult addNew( @RequestBody  CommentParam commentParam){
        System.out.println("commentParam = " + commentParam);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentParam,comment);
        System.out.println("comment = " + comment);
        commentService.addComment(comment);
        return JsonResult.ok();
    }
    @GetMapping("selectAll")
    @ApiOperation("查询评论列表")
    public JsonResult selectAllComment(){
        return JsonResult.ok(commentService.selectAllComment());
    }

    @GetMapping("selectById/{id}")
    @ApiOperation("根据ID查询")
    public JsonResult selectOne(@PathVariable Integer id){
        return JsonResult.ok(commentService.selectOne(id));
    }

    @GetMapping("deleteById/{id}")
    @ApiOperation("根据ID删除")
    public JsonResult deleteComment(@PathVariable Integer id){
        return JsonResult.ok(commentService.deleteComment(id));
    }

    @PostMapping("/updatecomment")
    @ApiOperation("修改评论")
    public JsonResult updateComment(  @RequestBody CommentParam commentParam){
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentParam,comment);
        System.out.println("comment = " + comment);
        commentService.UpdateComment(comment);
        return JsonResult.ok();
    }
}
